package com.tron.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AnnotationStart {
    private static HashMap<String, String> nameMap = new HashMap<>();
    static {
        nameMap.put("1", "tron1");
        nameMap.put("2", "tron2");
        nameMap.put("3", "tron3");
        nameMap.put("4", "tron4");
        nameMap.put("5", "tron5");
        nameMap.put("6", "tron6");
        nameMap.put("7", "tron7");
        nameMap.put("8", "tron8");
        nameMap.put("9", "tron9");
        nameMap.put("10", "tron10");
    }

    public static void main(String[] args) throws Exception {
        Tron tron1 = new Tron().setId("1").setOrgId("1").setDepId("1");
        Tron tron2 = new Tron().setId("2").setOrgId("1").setDepId("1");
        Tron tron3 = new Tron().setId("3").setOrgId("1").setDepId("1");
        Tron tron4 = new Tron().setId("4").setOrgId("1").setDepId("1");
        Tron tron5 = new Tron().setId("5").setOrgId("1").setDepId("1");
        Tron tron6 = new Tron().setId("6").setOrgId("1").setDepId("1");
        Tron tron7 = new Tron().setId("7").setOrgId("1").setDepId("1");
        Tron tron8 = new Tron().setId("8").setOrgId("1").setDepId("1");
        Tron tron9 = new Tron().setId("9").setOrgId("1").setDepId("1");
        Tron tron10 = new Tron().setId("10").setOrgId("1").setDepId("1");
        ArrayList<Tron> trons = new ArrayList<>();
        trons.add(tron1);
        trons.add(tron2);
        trons.add(tron3);
        trons.add(tron4);
        trons.add(tron5);
        trons.add(tron6);
        trons.add(tron7);
        trons.add(tron8);
        trons.add(tron9);
        trons.add(tron10);
        Instant now = Instant.now();
        List<Tron> tronList = prepareName(trons,nameMap);
        System.out.println(tronList);
        Instant end = Instant.now();
        System.out.println(Duration.between(now,end).toMillis());
    }

    public static <T> List<T> prepareName(List<T> list,HashMap<String,String> nameMap) throws Exception {
        final ArrayList<String> ids = new ArrayList<>();
        for (T t : list) {
            Class<?> aClass = t.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                IdToName idToName = field.getAnnotation(IdToName.class);
                if (null != idToName) {
                    String getMethodName = idToName.getMethod();
                    Method getMethod = aClass.getMethod(getMethodName);
                    String nameId = (String) getMethod.invoke(t);
                    ids.add(nameId);
                }
            }
        }
        System.out.println("获取到ids："+ids);
        // 查询缓存获取对应的名称
        for (T t : list) {
            Class<?> aClass = t.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field : declaredFields) {
                IdToName idToName = field.getAnnotation(IdToName.class);
                if (null != idToName) {
                    String getMethodName = idToName.getMethod();
                    String setMethodName = idToName.setMethod();
                    Method getMethod = aClass.getMethod(getMethodName);
                    Method setMethod = aClass.getMethod(setMethodName,String.class);
                    String nameId = (String) getMethod.invoke(t);
                    String reallyName = nameMap.get(nameId);
                    setMethod.invoke(t, reallyName);
                }
            }
        }
        return list;
    }
}
